<?php
namespace app\index\controller;

use think\Db;
use think\Session;
use think\View;
use think\Controller;

class Index extends Controller
{
    public function index()
    {
        $providerId = getProviderId();
//广告轮播
        $time=date('YmdHis');
        $s['startDateTime']=["<",$time];
        $e['endDateTime']=[">",$time];

        $ad=Db::name('advert')->where('providerId',$providerId)->where($s)->where($e)->join('files','advert.imgFileId=files.filesId')->select();
        foreach ($ad as $k => $value)
        {
            $thumbFile = $value['thumbFile'];
            $ad[$k]['thumbFile'] = str_replace("\\","/","$thumbFile");
        }
//        var_dump($ad);die;
        $this->view->assign('ad', $ad);
        //优惠服务
        $platBest=Db::name('service_best ')
            ->where('isEnable',1)->where('providerId',0)->order('service_best.addDateTime','asc')
            ->join('service ss','service.serviceId=service_best.serviceId')
            ->join('files','service_best.iconFileId = files.filesId ')
            ->field('thumbFile,ss.serviceId,activePrice,ss.serviceName')
            ->limit(3)
            ->select();
        $di=Db::name('service_best ')
            ->where('isEnable',1)->where('providerId',$providerId)->order('service_best.addDateTime','asc')
            ->join('service ss','service.serviceId=service_best.serviceId')
            ->join('files','service_best.iconFileId = files.filesId ')
            ->field('thumbFile,ss.serviceId,activePrice,ss.serviceName')
            ->limit(3)
            ->select();
        $count = count($di);
//        var_dump($count);die;
        if($count==1){
            $platBest[0]=$di[0];
        }elseif ($count==2){
            $platBest[0]=$di[0];
            $platBest[1]=$di[1];
        }elseif ($count>=3){

            $platBest[0]=$di[0];
            $platBest[1]=$di[1];
            $platBest[2]=$di[2];
        }
        $platBest[0]['thumbFile']='__STATIC__/images/simujijin.png';
        $platBest[1]['thumbFile']='__STATIC__/images/shenbanyiban.png';
        $platBest[2]['thumbFile']='__STATIC__/images/jingyingyichang.png';
        foreach($platBest as  $k => $v){
            $platBest[$k]['activePrice'] = detailPrice($v['activePrice']);
        }
//        var_dump($platBest);die;
        $this->view->assign('count',$count);
        $this->view->assign('di', $platBest);
        $data=Db::name('project')->where('status',1)->where(['project.projectType'=>3,'isSelled'=>0])->join('project_tradelicense','project.tradeLicenseId=project_tradelicense.tradeLicenseId')->join('project_sign','project.projectId=project_sign.projectId')->where(['project_sign.projectType'=>3])->where('signCode',4)->field('project.projectId,projectName,price')->order("case when memberId=0 then 1 ELSE 4 END", null)->limit(4)->select();
        foreach ($data as $key=>$value){
            $data[$key]['price'] = detailPrice($value['price']);
        }
        $this->view->assign('licenses',$data);
        //注册地址
        $map1['p.memberId'] = getProviderId();
        $map1['p.projectType'] = 1;
        $map1['p.status'] = 1;
        $map1['p.isSelled']=0;
        $registered = Db::name('project p')
            ->where($map1)
            ->join('project_registeraddress','project_registeraddress.registerAddressId=p.registerAddressId')
            ->order('p.addDateTime','desc')->limit(4)->select();
        if (count($registered)<4)
        {
            $registered = Db::name('project p')
                ->where('p.projectType',1)->where('p.status',1)->where('isSelled',0)
                ->join('project_registeraddress pr','p.registerAddressId=pr.registerAddressId')
                ->order('p.addDateTime','desc')->limit(4)->select();
        }
        foreach ($registered as $key => $value)
        {
            $registered[$key]['price'] = detailPrice($value['price']);
            //图标
            $registered[$key]['thumbFile']=Db::name('files')->where('filesId',$value['registerAddressFileId'])->value('thumbFile');
        }
        $this->view->assign('registered',$registered);
        //众创空间
        $map2['p.memberId'] = getProviderId();
        $map2['p.projectType'] = 2;
        $map2['p.status'] = 1;
        $map2['p.isSelled']=0;
        $weWork = Db::name('project p')
            ->field('p.projectId,p.projectName,p.projectType,p.location,p.price,p.isSelled')
            ->where($map2)->order('p.addDateTime','desc')->limit(4)->select();
        if (count($weWork)<4)
        {
            $weWork = Db::name('project p')
                ->field('p.projectId,p.projectName,p.projectType,p.price,p.location,p.isSelled')
                ->where('p.projectType',2)->where('p.status',1)->where('p.isSelled',0)
                ->order('p.addDateTime','desc')->limit(4)->select();
        }
        foreach ($weWork as $key => $value) {
            $weWork[$key]['price'] = detailPrice($value['price']);
            //图标
            $weWork[$key]['thumbFile'] = Db::name('project_gallery')->where('projectId', $value['projectId'])->join('files', 'files.filesId=project_gallery.imgFileId')->order('project_gallery.addDateTime', 'asc')->value('thumbFile');
        }
        $this->view->assign('weWork',$weWork);
        return view('index');
    }
}

